<?php
/**
 * Slipstream :: Highly adaptive dependency injection framework
 * Copyright 2010-2011 Ian Unruh (Xerof3ar)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
namespace Slipstream\Infrastructure\Parameter;

use ArrayObject;
use ReflectionMethod;
use ReflectionParameter;
use SplObjectStorage;
use Slipstream\Activation\SessionInterface;

/**
 * Base implementation of ResolverServiceInterface
 *
 * @author Ian Unruh (Xerof3ar) <ianunruh@gmail.com>
 * @since Class avaliable since release 1.0
 */
class ResolverService implements ResolverServiceInterface
{
	/**
	 * @var SplObjectStorage<ResolverInterface>
	 */
	protected $resolvers;
	
	/**
	 * Class constructor
	 * @return void
	 */
	public function __construct()
	{
		$this->resolvers = new SplObjectStorage();
	}
	
	/**
	 * Resolves the values to be injected into a given method
	 *
	 * @param ReflectionMethod $method
	 * @param SessionInterface $session
	 * @return ArrayObject
	 */
	public function resolveParameters(ReflectionMethod $method, SessionInterface $session)
	{
		$values = new ArrayObject();
		if($method->getNumberOfParameters() == 0) {
			return $values;
		}
		$parameters = $method->getParameters();
		foreach($parameters as $parameter) {
			$this->resolve($parameter, $session, $values);
		}
		return $values;
	}
	
	/**
	 * Registers a resolver with this service
	 *
	 * @param ResolverInterface $resolver
	 * @return void
	 */
	public function registerResolver(ResolverInterface $resolver)
	{
		$this->resolvers->attach($resolver);
	}
	
	/**
	 * Unregisters a resolver from this service
	 *
	 * @param ResolverInterface $resolver
	 * @return void
	 */
	public function unregisterResolver(ResolverInterface $resolver)
	{
		$this->resolvers->detach($resolver);
	}
	
	/**
	 * Returns the resolvers registered to this service
	 * @return SplObjectStorage<ResolverInterface>
	 */
	public function getResolvers()
	{
		return $this->resolvers;
	}
	
	/**
	 * Resolves the value for a parameter using the registered resolvers
	 *
	 * @throws LogicException If parameter is required and could not be resolved
	 * @param ReflectionParameter $parameter
	 * @param SessionInterface $session
	 * @param ArrayObject $values
	 * @return bool Returns true if value was resolved
	 */
	protected function resolve(ReflectionParameter $parameter, SessionInterface $session, ArrayObject $values)
	{
		$resolution = new ResolutionContext($parameter, $session);
		foreach($this->resolvers as $resolver) {
			$resolver->resolveValue($resolution);
			if($resolution->isResolved()) {
				$values->offsetSet($parameter->getPosition(), $resolution->getValue());
				return true;
			}
		}
		if(!$parameter->isOptional()) {
			throw new LogicException(sprintf('Could not resolve value for required parameter "%s"', $parameter->getName()));
		}
		return false;
	}
}